package com.example.t470p.facechecktickets;

import android.app.Application;
import android.content.Context;


import com.example.t470p.facechecktickets.Utils.CustomString;
import com.example.t470p.facechecktickets.Utils.LogUtil;
import com.example.t470p.facechecktickets.Utils.SPUtil;
import com.example.t470p.facechecktickets.Utils.ToastUtil;
import com.example.t470p.facechecktickets.Utils.pathUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;


public class MyApplication extends Application {


    private static Context mAppContext;
    static MyApplication myApplication = new MyApplication();
    public static  String m_Url, m_strGateNo;

    public static MyApplication getMyApplication() {
        return myApplication;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();
        //获取配置
        String ConfigPath = pathUtils.getSDPath() + "/config.ini";

        String FileName = "config.ini";

        File file = new File(pathUtils.getSDPath(),FileName);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        GetConfig(ConfigPath);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);


    }

    public static Context getAppContext() {
        return mAppContext;
    }

    /*
     * 获取配置信息
     */
    public  static boolean GetConfig(String ConfigPath) {
        boolean ret = false;
        try {
            FileInputStream fis = null; // 读
            Properties pp;

            pp = new Properties();
            fis = new FileInputStream(ConfigPath);
            pp.load(fis);

            m_Url = pp.get("SericeUrl").toString();
            m_strGateNo = pp.get("GateNO").toString();// 获取配置文件的Gps_device字段的信息，既它=号后面的数据

//            m_Url =  "192.168.20.33:8081/Base";
////            m_Url =  "115.223.34.46:18080";

            //判断JIN配置参数是否为空

            if (null ==m_Url || m_Url.equals("")){
                m_Url =  "192.168.20.33:8081/Base";
            }

            if (null ==m_strGateNo || m_strGateNo.equals("")){
                m_strGateNo =  "A001001W001" + "";
            }

            //如果获取到配置信息，那么久保存在Sp里面
            SPUtil.put(MyApplication.getAppContext(), CustomString.SP_NAME,CustomString.ZYFBASEURL, m_Url);
            SPUtil.put(MyApplication.getAppContext(), CustomString.SP_NAME,CustomString.ZYFGATENUMBER,m_strGateNo);
            SPUtil.put(MyApplication.getAppContext(), CustomString.SP_NAME,CustomString.ZYFUSERNAME,"");
            SPUtil.put(MyApplication.getAppContext(), CustomString.SP_NAME,CustomString.ZYFPASSWORDD,"");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}